package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author shkstart
 * @create 2023-03-31 10:17
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    // 注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    // 查询表所有数据
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("getAll")
    public Result getAll(){
        try {
            int a = 1 / 0;
        } catch (Exception e) {
            throw new YyghException("失败", 201);
        }
        // 查询表中所有记录
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    // 删除医院
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {
        return Result.ok(hospitalSetService.removeById(id));
    }

    // 条件查询带分页
    // RequestBody表示用json形式传数据，required = false表示可以为空
    // 使用RequestBody注解时，必须使用PostMapping
    @ApiOperation(value = "条件查询医院带分页")
    @PostMapping ("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        // 创建page对象
        Page<HospitalSet> page = new Page<>(current, limit);
        // 条件构造
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        // 判断是否为空
        if (!StringUtils.isEmpty(hosname)) wrapper.like("hosname", hosname);
        if (!StringUtils.isEmpty(hoscode)) wrapper.eq("hoscode", hoscode);
        // 调用接口
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);
        return Result.ok(hospitalSetPage);
    }

    // 添加医院
    @ApiOperation(value = "添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        // 设置状态（1：可用，0：不可用）
        hospitalSet.setStatus(1);
        // 签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        // 调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) return Result.ok();
        return Result.fail();
    }

    // 根据id获取医院设置
    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("getHospSetById/{id}")
    public Result getHospSetById(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    // 修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PostMapping("updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet) {
        boolean update = hospitalSetService.updateById(hospitalSet);
        if (update) return Result.ok();
        return Result.fail();
    }

    // 批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemoveHospSet")
    public Result batchRemoveHospSet(@RequestBody List<Long> idList){
        boolean remove = hospitalSetService.removeByIds(idList);
        if (remove) return Result.ok();
        return Result.fail();
    }

    // 医院设置解锁和锁定
    @ApiOperation(value = "医院设置解锁和锁定")
    @PutMapping("lockHospSet/{id}/{status}")
    public Result lockHospSet(@PathVariable Long id, @PathVariable Integer status) {
        // 获取要修改锁定状态的医院
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 设置状态值
        hospitalSet.setStatus(status);
        // 调用接口，进行修改
        boolean update = hospitalSetService.updateById(hospitalSet);
        // 判断是否修改成功
        if (update) return Result.ok();
        return Result.fail();
    }

    // 发送签名密钥
    @ApiOperation(value = "发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id) {
        // 根据id获取医院信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 得到密钥和医院编号
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        //TODO 发送短息
        return Result.ok();
    }
}
